﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Threading.Tasks;

namespace CloudStorageLight.WpfClient
{
    /// <summary>
    /// フォルダTreeViewのアイテム
    /// </summary>
    public class DirectoryItem : TreeItem
    {
        public ObservableCollection<DirectoryItem> Folders
        {
            get 
            {
                return _Folders; 
            }
        }
        private ObservableCollection<DirectoryItem> _Folders;


        public ObservableCollection<FileItem> Files
        {
            get { return _Files; }
        }
        private ObservableCollection<FileItem> _Files;

        public TreeItem GetSelectedNode()
        {
            if (IsSelected) return this;
            foreach (var child in Folders)
            {
                var node = child.GetSelectedNode();
                if (node != null) return node;
            }
            return null;
        }

        public DirectoryItem()
        {
            _Folders = new ObservableCollection<DirectoryItem>();
            _Folders.Add(new DirectoryItem(true) { Title="Loading..." });
            _Files = new ObservableCollection<FileItem>();
            Icon = "/CloudStorageLight;Component/Images/folder_explore.png";
            Type = "Directory";
        }

        protected DirectoryItem(bool dummy)
        {
            _Folders = new ObservableCollection<DirectoryItem>();
            _Files = new ObservableCollection<FileItem>();
            Icon = "/CloudStorageLight;Component/Images/folder_explore.png";
        }

        public override string Icon
        {
            get
            {
                if (LoadedChildren)
                {
                    return "/CloudStorageLight;Component/Images/folder.png";
                }
                else
                {
                    return "/CloudStorageLight;Component/Images/folder_explore.png";
                }
            }
            set
            {
                base.Icon = value;
            }
        }

        public bool LoadedChildren
        {
            get { return _loadedChildren; }
            set { _loadedChildren = value; }
        }
        private bool _loadedChildren;

        public virtual async Task LoadChildrenAsync(bool force = false)
        {
            if (!LoadedChildren || force)
            {
                LoadedChildren = true;
                _Folders.Clear();
                _Files.Clear();

                var result = await BlobDataManager.Instance.GetFilesAsync(Path, 0);
                foreach (TreeItem item in result.OrderBy(a => a.Title, StringComparer.CurrentCultureIgnoreCase))
                {
                    if (item.Type == "Directory")
                    {
                        Folders.Add((DirectoryItem)item);
                    }
                    else
                    {
                        Files.Add((FileItem)item);
                    }
                }

                OnPropertyChanged("Folders", "Files", "Icon");
            }
        }

        //public bool IsLocalSync
        //{
        //    get { return BlobDataManager.Instance.HasMappingFolder(Path); }
        //}

        //public bool IsMappingFolder
        //{
        //    get { return BlobDataManager.Instance.GetMappingFolder(Path) != null; }
        //}

        //public bool CanSync
        //{
        //    get { return IsMappingFolder && !BlobDataManager.Instance.Offline; }
        //}

        public virtual bool HasContextMenu
        {
            get { return true; }
        }
        public virtual bool IsFavoriteItem
        {
            get { return false; }
        }
        public virtual bool IsNotFavoriteItem
        {
            get { return !IsFavoriteItem; }
        }
    }

    public class FavoriteDirectoryItem : DirectoryItem
    {
        public FavoriteDirectoryItem():base(true)
        {
            LoadedChildren = true;
        }

        public override Task LoadChildrenAsync(bool force = false)
        {
            return Task.Delay(0);
        }

        public override bool HasContextMenu
        {
            get { return false; }
        }
    }

    public class FavoriteItem : DirectoryItem
    {
        public FavoriteItem() : base()
        {
        }

        public override bool IsFavoriteItem
        {
            get { return true; }
        }
    }

}
